//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import goodsItem from '../comm/goodsItem.vue';
import sponPopup from '../comm/sponPopup.vue';
import exchangePopup from '../comm/exchangePopup.vue';
import { scrollMixin } from "utils/mixin";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {
    goodsItem,
    sponPopup,
    exchangePopup
  },
  mixins: [scrollMixin], //加载更多
  data() {
  return {
    sponShow:false,
    changeShow:false,
    page: 1,
    isLoadMore: true,
    total_page: 0,
    maxtotal:'',
    params:{
      transfer_id:'',
      goods_id:'',
      total:1
    },
    addressName:'',
    exParams:{
      total:1,
      goods_id:"",
      address:{
        province:"",
        city:"",
        district:"",
        mobile:"",
        username:'',
        street:'',
        address:''
      }
    },
    myList:[],
    timeout:''
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    sponsorClick(goodsId,maxtotal) {
      this.sponShow = true;
      this.params.goods_id = goodsId;
      this.maxtotal = Number(maxtotal);
    },
    covertClick(goodsId,maxtotal) {
      this.changeShow = true;
      this.exParams.goods_id = goodsId;
      this.maxtotal = Number(maxtotal);
    },
    exchangeClick() {
      if(this.maxtotal == 0) {
        this.$toast('没有可转赠的藏品');
        return false;
      }
      if(this.fun.isTextEmpty(this.exParams.total)) {
        this.$toast('商品数量错误');
        return false;
      }
      if(this.fun.isTextEmpty(this.exParams.address.username)) {
        this.$toast('请输入收件人');
        return false;
      }
      if(this.fun.isTextEmpty(this.exParams.address.mobile)) {
        this.$toast('请输入手机号');
        return false;
      }
      if(this.fun.isTextEmpty(this.exParams.address.province) || this.fun.isTextEmpty(this.exParams.address.city) || this.fun.isTextEmpty(this.exParams.address.district)) {
        this.$toast('请输入地址');
        return false;
      }
      if(this.fun.isTextEmpty(this.exParams.address.address)) {
        this.$toast('请输入详细地址');
        return false;
      }
      if (this.timeout) {
        clearTimeout(this.timeout);
      }
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
      });
      this.timeout = setTimeout(() => {
        this.exchangeFn();
      }, 300);
    },
    exchangeFn() {
      $http.post("plugin.collection-center.frontend.controllers.collection.collection-manage.exchange", this.exParams)
        .then((response) => {
          if (response.result == 1) {
            this.$toast(response.msg);
            this.changeShow = false;
            this.initData();
            this.collectionList();
            this.$toast.clear();
          } else {
            this.$toast(response.msg);
          }
      })
      .catch((err) => {
        console.log(err)
      });
    },
    initData() {
      this.page = 1;
      this.addressName = "";
      this.params = {
        transfer_id:'',
        order_id:'',
        total:1
      };
      this.exParams = {
        total:1,
        order_id:"",
        address:{
          province:"",
          city:"",
          district:"",
          mobile:"",
          username:'',
          street:'',
          address:''
        }
      };
    },
    sponsorConfrim() {
      if(this.fun.isTextEmpty(this.params.goods_id)) {
        this.$toast('订单ID错误');
        return false;
      }
      this.params.transfer_id = Number(this.params.transfer_id);
      if(this.fun.isTextEmpty(this.params.transfer_id)) {
        this.$toast('会员ID错误');
        return false;
      }
      if(this.fun.isTextEmpty(this.params.total)) {
        this.$toast('商品数量错误');
        return false;
      }
      
      $http.post("plugin.collection-center.frontend.controllers.collection.collection-manage.transfer", this.params)
        .then((response) => {
          if (response.result == 1) {
            this.$refs.sponPopup.memberFlag = false;
            this.$toast(response.msg);
            this.initData();
            this.collectionList();
          } else {
            this.$toast(response.msg);
          }
      })
      .catch((err) => {
        console.log(err)
      });
    },
    addressCallback(obj) {
      this.addressName = obj.itemName1 + " " + obj.itemName2 + " " + obj.itemName3;
      this.exParams.address.province = obj.itemName1;
      this.exParams.address.city = obj.itemName2;
      this.exParams.address.district = obj.itemName3;
    },
    streetConfirm(obj) {
      this.exParams.address.street = obj.areaname;
    },
    collectionList() {
      $http.get('plugin.collection-center.frontend.controllers.collection.collection-manage', {page:this.page}, "加载中..").then(response => {
        if (response.result == 1) {
            let res = response.data;
            let list = res.collection_list.data;
            this.isLoadMore = true;
            this.total_page = res.collection_list.last_page;
            if (!this.total_page) {
              this.page = 0;
            }
            this.myList = list;
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let query = {
          page: this.page,
          basic_info: 1
        };
        $http.post("plugin.collection-center.frontend.controllers.collection.collection-manage", query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let data = res.data;
              let list = data.collection_list.data;
              this.myList = [...this.myList, ...list]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.collectionList();
    this.$refs.exchangePop.getIsOpenAddress(); //选择地址的开关接口
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}